import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text ,RichText,Button,ScrollView } from '@tarojs/components'
import CommonJs from '../../utils/common'
import '../../assets/common.scss'
export default class Hearder extends Component {	
 constructor(props) {
    super(props);
    this.state = {
		userinfo:Taro.getStorageSync('userinfo'),
      result: [],
      myTime:null,
      navwidth:0,
      nav:[]
    }
  }

  componentWillMount () {
  	this.getdh()
   var id=Taro.getStorageSync('dateinfo').qid;
  	if(!id){
  		CommonJs.open('/pages/grouplist/grouplist')
  	}
    var _this = this
    var navwidths = Taro.getStorageSync('navwidths')
    // console.log(navwidths+'//1111111111111111111111')
    if (navwidths != '' || navwidths != 'undefined') {
      if (navwidths == 1) {
        Taro.getSystemInfo({
          success:function(res){
            var windowWidth = res.windowWidth
            var navwidth = windowWidth / 6
            // console.log(navwidth+'2222222222222222222222')
            _this.setState({
              navwidth:navwidth
            })
          }
        })
      }
    }
  }
  componentDidShow () {
    var _this = this
    clearTimeout(_this.myTime)
    var navwidths = ''
    Taro.setStorage({key:'navwidths',data:navwidths})
    var chatRoomTypeIds
    // console.log(_this.props.porpNav+'////1111111111111111111111111')
    if(_this.props.porpNav == 1 || _this.props.porpNav == 6 || _this.props.porpNav == 9){
      chatRoomTypeIds = 2
    }else if(_this.props.porpNav == 3){
      chatRoomTypeIds = 4
    }else if(_this.props.porpNav == 4){
      chatRoomTypeIds = 5
    }else if(_this.props.porpNav == 7){
      chatRoomTypeIds = 6
    }
    var cid = Taro.getStorageSync('dateinfo').qid
    // console.log(Taro.getStorageSync('dateinfo')+'yy000000000000')
    if (cid != ''){
      // console.log('gfgfgfgffffffffffffffff')
      //登陆没有群id跳群列表判断因为活动头部会请求
      _this.getChatHistory(chatRoomTypeIds)
    }
    
  }
  shouldComponentUpdate () {
  }
  componentWillUnmount () {
    clearTimeout(this.myTime)
    // clearInterval(this.myTime1)
  }

  opens(url,type){
    var types=0
    var ptype=''
    // clearInterval(this.myTime)
    //缓存一个值判断横向是否滚动一段距离
    if(url==1){
    	var curl='/pages/activity/activity?scene='+this.props.propItem.id
    }
    else if(url==2){
    	var curl='/pages/chat/syslist'
    	ptype=2
    }
    else if(url==3){
    	var curl='/pages/index/index'
    	ptype=1
    }
    else if(url==4){
    	var curl='/pages/info/info'
    	types=1;
    }
    var navwidths = types
    Taro.setStorage({key:'navwidths',data:navwidths})
    this.props.onHandOpen(curl,ptype)
  }
  opendetails(url){
    CommonJs.openTo(url);
  }
  getdh(){
  	var url='/advert/papi/v1.9/part/byStatus/1'
  	var _this=this
  	CommonJs.ajaxRequest(url,'','GET',function(res){
		_this.setState({
          nav: res.data 
        })
		
    },"wfw")
  }
  openchat(url,id){
    var _this=this
    if(_this.props.porpNav == 1 || _this.props.porpNav == 6 || _this.props.porpNav == 9){
      _this.$preload({
        'chatRoomTypeId':2
      });
    }else if(_this.props.porpNav == 3){
      _this.$preload({
        'chatRoomTypeId':4
      });
    }else if(_this.props.porpNav == 4){
      _this.$preload({
        'chatRoomTypeId':5
      });
    }else if(_this.props.porpNav == 7){
      _this.$preload({
        'chatRoomTypeId':6
      });
    }
    CommonJs.openTo(url);
  }
  
  getChatHistory(obj){
    //获取群聊天历史记录
//  var _this=this
//  var dates
//   dates={
//      clusterId:Taro.getStorageSync('dateinfo').qid,
//      pageNumber:1,
//      sortColumns:'id desc',
//      pageSize:3,
//      chatRoomTypeId: obj
//    }
//  // console.log(_this.props.porpNav+'oooooooooo1111111888888888888')
//  // console.log(JSON.stringify(dates)+'1111111888888888888')
//  CommonJs.ajaxRequest('/clusterlog/getChatlogOrNotLogin',dates,'GET',function(res){
//      // console.log(JSON.stringify(res)+'111111199999999999')
//      if (res) {
//        var results = res.result
//        var result = results.reverse()
//        _this.setState({
//           result: result
//        })
//        _this.myTime = setTimeout(() => { _this.getChatHistory(obj) }, 6000)
//        // _this.roll()
//      } else {
//        _this.myTime = setTimeout(() => { _this.getChatHistory(obj) }, 6000)
//      }
//  },'noloading')
  }
  roll(){
    //轮播暂时没用
    var _this = this
    var lbheight = 0
    _this.myTime1 = setInterval(() => {
      lbheight = lbheight - 62
      console.log('yyyyyyy')
      if (lbheight == 248) {
        _this.setState({
          lotnlnbotop: 0 
        })
      } else {
        _this.setState({
          lotnlnbotop: lbheight 
        })
      }
    }, 1500)
    _this.myTime = setTimeout(() => { _this.getChatHistory(obj) }, 6000)
  }
  changeyhq(item){
    let str = ''
    let contents = item.content
    let index = contents.indexOf("|||")//是否包含
    let bosh = null
    let markUrl = null
    let markUrls = []
    let sendObjectId = item.sendObjectId
    if(sendObjectId == 11){//群管后台活动列表分享活动 
      markUrl = item.markUrl
    }
    if(markUrl){//群管后台活动列表分享活动 
      markUrls = markUrl.split("|||")
    }
    if(index >= 0){
      bosh = contents.split("|||")
    }
    if (markUrls[0] == 1012 && markUrls[1] == "project") {
      str = '活动'
    } else if (index < 0) {
      str = contents
    } else if (bosh[1] == "pic") {
      str = '图片'
    } else if (bosh[1] == "ActivityShare") {
      str = '活动'
    } else if (bosh[1] == "pkgShare") {
      str = '商品'
    } else if(bosh[1] == "yuyin") {
      str = '语音'
    } else if(bosh[1] == "freegouShare") {
      str = '商品'
    } else {
      str = contents
    }
    return str
  }
  render () {
    const qun_date = this.props.propItem
    const navindex = this.props.porpNav
    const isyd=this.props.propGuite
    const result = this.state.result
    const nav=this.state.nav

//  <Text className={navindex==4?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/activity/activity?scene='+qun_date.id,'','0')}>活动</Text>
//  <Text className={navindex==3?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/chat/syslist','2','0')}>说一说</Text>
//  <Text style="display:none;" className={navindex==9?isyd==2?'nav cur tsrelative':'nav cur':isyd==2?'nav tsrelative':'nav'} onClick={this.opens.bind(this,'/pages/pkgo/lottery','','0')}>抽奖</Text>
//  <Text className={navindex==1?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/index/index','1','0')}>动态</Text>
//	<Text className={navindex==3?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/index/index','2','1')} style="display:none;">说一说</Text>
//  <Text className={navindex==7?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/pkgo/pkgo','','1')} style="display:none;">免费购</Text>
//  <Text className={navindex==10?'nav cur':'nav'} onClick={this.opendetails.bind(this,'/pages/mybill/mybill')} style="display:none;">买单</Text>
//  <Text className={navindex==6?'nav cur':'nav'} onClick={this.opens.bind(this,'/pages/info/info','','1')}>资料</Text>
//  <Text  style="display:none;" className={navindex==8?'nav cur':'nav'} onClick={this.opendetails.bind(this,'/pages/my/my')}>我的</Text>
    const userid = Taro.getStorageSync('dateinfo').userid
    return(
      <View className='qun-commm'>
        <View className='qun-banner'>
          <View className='qun-ban'>
            <View className='qun-lotngs'>
              <View className='qun-lotngzg' onClick={this.openchat.bind(this,'/pages/chat/chat',navindex)}>
                <View className='qun-lotnlunbo'>
                  {result.map((item, index) => (
                   <View className={index == 0 ?'qun-lotnyito qun-lotnleft':'qun-lotnyito qun-lotnright'}>
                    <Image className='qun-lotnimgs' mode='scaleToFill' src={item.photo?item.photo:'https://minichat.g2ma.com/kedouimg/A6984900727459logo.png'}></Image>
                    <View className='qun-lotninfo'>{this.changeyhq(item)}</View>
                   </View>
                  ))}
                </View>
              </View>
            </View>
            <Image className='ban-img' mode='widthFix' src={qun_date.backgroundImg}></Image>
          </View>
          <Image className='qun-img' mode='scaleToFill' src={qun_date.photo}></Image>
          <View className='qun-tit'>
            <View>
              <Text className='qun-cynum'>参与:{qun_date.clusterCount}人</Text>
              <Text className='qun-cyno'>ID:{qun_date.id}</Text>
            </View>
            <View>
              <Button openType='share' className='qun-fnxn buttons'>
                <Image className='qun-fnxnimg' mode='scaleToFill' src='https://minichat.g2ma.com/kedouimg/40.png'></Image>
                <Text className='qun-fxwn'>分享</Text>
             </Button>
            </View>
          </View>
		  <View  style="display:none;" className={isyd==4?'qun-md tsrelative02':'qun-md'} onClick={this.opendetails.bind(this,'/pages/mybill/mybill')}>
                <Image className='qun-fnxnimg' mode='scaleToFill' src='https://minichat.g2ma.com/kedouimg/wx01.png'></Image>
                <Text className='qun-fxwn'>我要买单</Text>
             </View>
          <View className='qun-funbor'>
            <ScrollView 
              className='qun-fung' 
              scrollX='true'
              scrollLeft={this.state.navwidth}>
              <View className='qun-fun'>
              	{nav.map((item, index) => (
              		
                <Text className={navindex==index?'nav cur':'nav'} onClick={this.opens.bind(this,item.id)}>{item.name}</Text>
                ))}
              	
              	
              	
              </View>
            </ScrollView>
          </View>
        </View>
      </View>
    )
  }
}